package com.gmail.huxiaowi.cblog.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gmail.huxiaowi.cblog.web.freemarker.FreemarkerSupport;

import freemarker.template.Template;

@Controller
@RequestMapping({ "/", "/index/*" })
public class PageController extends FreemarkerSupport{

    @RequestMapping 
    public ModelAndView handleRequest(HttpServletRequest req,
            HttpServletResponse resp) throws Exception {
        Template tl = getTemplate(getServletContext(req), "/ftl", "page.ftl");

        String requestURI = req.getRequestURI();
        logger.debug(requestURI);
        String pageNoStr = requestURI.substring(requestURI.lastIndexOf("/") + 1);
        
        Map<String, Object> dataModel = new HashMap<String, Object>();
        dataModel.put("articles", userBiz.queryAllArticles("".equals(pageNoStr) ? 1 : Integer.parseInt(pageNoStr)));
        tl.process(dataModel, resp.getWriter());

        return null;
    }

}
